<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item :label="`${$gradeName?$gradeName:'班级'}名称`" prop="className">
        <el-input
          v-input-restrict
          v-model="queryParams.className"
          :placeholder="`请输入${$gradeName?$gradeName:'班级'}名称`"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="`${$branchName?$branchName:'分院'}名称`" prop="branchId">
        <el-select
          filterable
          v-model="queryParams.branchId"
          :placeholder="`${$branchName?$branchName:'分院'}名称`"
          clearable
        >
          <el-option
            v-for="(item, index) in branchs"
            :key="index"
            :label="item.branchName"
            :value="item.branchId"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="`${$majorName?$majorName:'专业'}名称`" prop="majorId">
        <el-select
          filterable
          v-model="queryParams.majorId"
          :placeholder="`${$majorName?$majorName:'专业'}名称`"
          clearable
        >
          <el-option
            v-for="(item, index) in majors"
            :key="index"
            :label="item.majorName"
            :value="item.majorId"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button v-preventReClick
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button v-preventReClick icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="postList">
      <el-table-column :label="`${$gradeName?$gradeName:'班级'}名称`" align="center" prop="postId" />
      <el-table-column :label="`${$branchName?$branchName:'分院'}名称`" align="center" prop="postCode" />
      <el-table-column :label="`${$majorName?$majorName:'专业'}名称`" align="center" prop="postName" />
      <el-table-column :label="`${$gradeName?$gradeName:'班级'}人数`" align="center" prop="postSort" />
      <el-table-column :label="`已选${$courseName?$courseName:'课程'}数量`" align="center" prop="postSort" />
      <el-table-column
        label="最后操作时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button v-preventReClick
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleUpdate(scope.row)"
            >详情</el-button
          >
          <el-button v-preventReClick
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form @submit.native.prevent ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="8">
            <el-form-item :label="`${$gradeName?$gradeName:'班级'}名称`" prop="postName"> </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="`${$branchName?$branchName:'分院'}名称`" prop="postName"> </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="`${$majorName?$majorName:'专业'}名称`" prop="postName"> </el-form-item>
          </el-col>
        </el-row>
        <div class="titleLineGreen" style="margin: 0 0 15px 0">已选{{ $courseName?$courseName:'课程' }}</div>
        <el-table v-loading="loading" :data="postList">
          <el-table-column :label="`${$courseName?$courseName:'课程'}`" align="center" prop="postId" />
          <el-table-column label="编号" align="center" prop="postCode" />
          <el-table-column label="主讲老师" align="center" prop="postName" />

          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button v-preventReClick
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleDiaListDelete(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div class="titleLineGreen" style="margin: 25px 0 15px 0">添加{{ $courseName?$courseName:'课程' }}</div>
        <el-select
          v-model="form.courseId"
          :placeholder="`添加${$courseName?$courseName:'课程'}`"
          clearable
          filterable
        >
          <el-option
            v-for="(item, index) in courses"
            :key="index"
            :label="item.courseName"
            :value="item.courseId"
          />
        </el-select>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-preventReClick @click="cancel">取 消</el-button>
        <el-button v-preventReClick type="primary" @click="submitForm">确 定</el-button>

      </div>
    </el-dialog>
  </div>
</template>

<script>
//TODO 只写了全局模板
import {
  listPost,
  getPost,
  delPost,
  addPost,
  updatePost,
} from "@/api/system/post";
import {
  listPost_branch,
  listPost_major,
} from "@/api/schoolOrganization/organization";
import { listPost_course } from "@/api/course/course";
export default {
  name: "Post",
  dicts: ["sys_normal_disable"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        className: undefined,
        branchId: undefined,
        majorId: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},

      branchs: [],
      majors: [],
      courses: [],
    };
  },
  created() {
    this.getList();
    this.getBranch();
    this.getCourse();
  },
  methods: {
    getCourse() {
      listPost_course().then((res) => {
        this.courses = res.rows;
      });
    },
    getBranch() {
      listPost_branch().then((res) => {
        this.branchs = res.rows;
      });
    },
    // /* 分院下拉变化 */
    // branchChange(e) {
    //   listPost_major({ branchId: e }).then((res) => {
    //     this.majors = res.rows;
    //   });
    // },
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      listPost(this.queryParams).then((response) => {
        this.postList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        postId: undefined,
        postCode: undefined,
        postName: undefined,
        postSort: 0,
        status: "0",
        remark: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const postId = row.postId || this.ids;
      getPost(postId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "编辑";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.postId != undefined) {
            updatePost(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPost(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleDiaListDelete(row) {
      const postIds = row.postId || this.ids;
      this.$modal
        .confirm("确认删除吗？")
        .then(function () {
          return delPost(postIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
